//
//  BATableViewController.m
//  myDemo2
//
//  Created by wuxueying on 5/13/14.
//
//

#import "BATableViewController.h"
#import "BATableView.h"

@interface BATableViewController ()<BATableViewDelegate>
@property (nonatomic, strong) BATableView *contactTableView;
@property (nonatomic, strong) NSArray * dataSource;
@end
@implementation BATableViewController

- (void) createTableView {
    self.contactTableView = [[BATableView alloc] initWithFrame:self.view.bounds];
    self.contactTableView.delegate = self;
    [self.view addSubview:self.contactTableView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.dataSource = @[@{@"indexTitle": @"小学",
                          @"data":@[@"伍雪颖", @"alfred", @"ain", @"abdul", @"anastazja", @"angelica"]},
                        @{@"indexTitle": @"中学",
                          @"data":@[@"dennis" , @"deamon", @"destiny", @"dragon", @"dry", @"debug", @"drums"]},
                        @{@"indexTitle": @"高中",
                          @"data":@[@"Fredric", @"France", @"friends", @"family", @"fatish", @"funeral"]},
                        @{@"indexTitle": @"大学",
                          @"data":@[@"Mark", @"Madeline"]},
                        @{@"indexTitle": @"研究生",
                          @"data":@[@"Nemesis", @"nemo", @"name"]}];
    [self createTableView];
}

- (NSArray *) sectionIndexTitlesForABELTableView:(BATableView *)tableView {
    NSMutableArray * indexTitles = [NSMutableArray array];
    for (NSDictionary * sectionDictionary in self.dataSource) {
        [indexTitles addObject:sectionDictionary[@"indexTitle"]];
    }
    return indexTitles;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.dataSource[section][@"indexTitle"];
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataSource[section][@"data"] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString * cellName = @"UITableViewCell";
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
    }
    cell.textLabel.text = self.dataSource[indexPath.section][@"data"][indexPath.row];
    return cell;
}

@end
